<?php

/**
 * @version		$Id: hello.php 15 2009-11-02 18:37:15Z chdemko $
 * @package		Joomla16.Tutorials
 * @subpackage	Components
 * @copyright	Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
 * @author		Christophe Demko
 * @link		http://joomlacode.org/gf/project/helloworld_1_6/
 * @license		License GNU General Public License version 2 or later
 */

/* No direct access to this file */
defined('_JEXEC') or die('Restricted access');

/* import Joomla modelitem library */
jimport('joomla.application.component.modelitem');

/* class bgmodelbg */
class bgModelbg extends JModel
{
    var $_total = null;
    var $_pagination = null;
    
    public function __construct(){
    	parent::__construct();
 
        $mainframe = JFactory::getApplication();
 
        /* Get pagination request variables */
        $limit = $mainframe->getUserStateFromRequest('com_information.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
        $limitstart = JRequest::getVar('limitstart', 0, '', 'int');
 
        /* In case limit has been changed, adjust it */
        $limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);
 
        $this->setState('limit', $limit);
        $this->setState('limitstart', $limitstart);
    }
    
    /* get all records docm */
	function getAll($list = null){
        $qr = "SELECT * FROM docm";
        
        $where = array();
       /* search */
       
       if(isset($list['filter_search'])){
            $where[] = "DOCM_NAME LIKE '%$list[filter_search]%'";
       } 
       
       if($where){
            $where = ' WHERE '.implode(' AND ', $where);
        }else{
            $where = null;
        }
       /* get total number record */
       $this->_total = $this->getTotal($where);
       
       $qr .= $where;
       /* order by */
       $qr .= " ORDER BY DOCM_CODE DESC"; 
       
       /* pagination */
       $qr .= " LIMIT ".$this->getState('limitstart').",".$this->getState('limit');
       /* load data */
       $this->_db->setQuery($qr);
       $result = $this->_db->loadObjectList();
 
       return $result;    
	}

    /* get total records docm */
    function getTotal($where = null){
        /* Load the content if it doesn't already exist */
        if ($this->_total === null) {
            $query = "SELECT count(*) FROM docm $where";
            $this->_db->setQuery($query);
            $this->_total = $this->_db->loadResult();
        }
        
        return $this->_total;
    } 
    
    /* get pagination */
    function getPagination(){
        /* Load the content if it doesn't already exist */
        if (empty($this->_pagination)) {
            jimport('joomla.html.pagination');
            $this->_pagination = new JPagination($this->getTotal(), $this->getState('limitstart'), $this->getState('limit') );
        }
        
        return $this->_pagination;
    }
    
    /* upload file csv */
    function uploadCSV($result){
        /* delete all records from DOCM table */
        $query = "DELETE FROM docm";
  
        $this->_db->setQuery($query);
        $this->_db->query();
        
        foreach($result as $key=>$row){
            /* skip first row */
            if($key == 0){
                continue;
            }
            /* insert values to DOCM table */
            $qr = "INSERT INTO docm (DOCM_CODE, DOCM_NAME) VALUES ('$row[0]', '$row[1]')";
            $this->_db->setQuery($qr);
            $this->_db->query();
         } 
    }
    
    /* remove items docm */
    public function remove($cid){
        $cid = implode(',', $cid);
    
        $query = "DELETE FROM docm WHERE DOCM_CODE IN($cid)";
  
        $this->_db->setQuery($query);
        $this->_db->query();
    }
}
